package com.woniu.service;

import com.woniu.adapter.DailyCodeValuationAdapter;
import com.woniu.adapter.ProductBuyAdapter;
import com.woniu.inlet.web.fo.FixedInputBuyFo;
import com.woniu.inlet.web.fo.FixedInputBuyOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 定投产品类
 */
@Service
@Transactional
@Slf4j
public class FixedInputService {

    @Autowired
    private ProductBuyAdapter buyAdapter;

    @Autowired
    private DailyCodeValuationAdapter codeAdapter;

    /**
     * 定投产品的确认
     *
     * @param fo
     */
    public String buyFixed(FixedInputBuyFo fo) {
        String code = codeAdapter.findCode(fo.getCodeId());
        if (code.equals("n")) {
            return "n";
        }
        buyAdapter.fixedInputBuy(fo);
        return "y";
    }

    /**
     * 定投产品的确认
     *
     * @param fixedInputBuyOrder
     */
    public void confirmBuyOrder(FixedInputBuyOrder fixedInputBuyOrder) {
        if (fixedInputBuyOrder.getRevocationTime().equals("0")) {
            //说明余额不足，删除产生的定投买入单子,并改定投状态，等待14点的定投
            buyAdapter.delFixedInputBuyOrder(fixedInputBuyOrder);
        } else {
            buyAdapter.confirmFixedInputBuyOrder(fixedInputBuyOrder);
            log.debug("定投的确认，确认可撤销的的时间为{}", fixedInputBuyOrder.getRevocationTime());
        }

    }
}
